<?php
namespace App\Controller\Admin;

use App\Query\MallGoodsQuery;
use App\Repository\MallGoodsRepository;
use App\Request\MallGoodsRequest;

class MallGoodsController extends Controller
{
    // 列表
    public function index()
    {
        return MallGoodsQuery::query(
            MallGoodsQuery::model(),
            $this->inputs('name', 'mall_id', 'money_pay', 'money_shop', 'money_point')
        )->paginate(15)->toArray();
    }
    
    // 新增
    public function create(MallGoodsRequest $request)
    {
        if(MallGoodsRepository::make()->create($request->validated()))
        {
            $this->message('create');
        }
        
        $this->error('create');
    }
    
    // 更新
    public function update($id, MallGoodsRequest $request)
    {
        $model = MallGoodsRepository::find($id);

        if($model && $model->save($request->validated()))
        {
            $this->message('update');
        }
        
        $this->error('update');
    }
    
    // 删除
    public function delete($id)
    {
        $model = MallGoodsRepository::find($id);

        if($model && $model->delete())
        {
            $this->message('delete');
        }
        
        $this->error('delete');
    }
    
    // 审核商品
    public function status($id)
    {
        $model = MallGoodsRepository::find($id);

        if($model && $model->setStatus((int) $this->input('status'), (int) $this->input('sort'), (float) $this->input('money')))
        {
            $this->message('ok');
        }
        
        $this->error('ok');
    }
}